package com.iterator.controller.wechat.wy.address;

import com.iterator.bean.web.pojo.BaseRespVo;
import com.iterator.bean.wechat.wy.address.AddressBO;
import com.iterator.bean.wechat.wy.address.AddressDetailVO;
import com.iterator.bean.wechat.wy.address.AddressId;
import com.iterator.bean.wechat.wy.address.AddressVO;
import com.iterator.service.wechat.wy.address.WxAddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: Silence_W
 * @date: 2021-01-12 17:32
 **/
@RestController
@RequestMapping("wx/address")
public class AddressController {

    @Autowired
    WxAddressService wxAddressService;

    //回显全部地址信息
    @RequestMapping("list")
    public BaseRespVo list(){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
       List<AddressVO> addressVO = wxAddressService.queryAddress(username);
        return BaseRespVo.ok(addressVO);
    }

    //查询单个地址信息
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id){
       AddressDetailVO addressDetailVO= wxAddressService.queryAddressDetailById(id);
       return BaseRespVo.ok(addressDetailVO);
    }
    //编辑指定地址信息
    @RequestMapping("save")
    public BaseRespVo save(@RequestBody AddressBO addressBO){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        if (addressBO.getId()==0){
            int id= wxAddressService.insertAddress(addressBO,username);
            return BaseRespVo.ok(id);
        }
        wxAddressService.updateAddressDetail(addressBO);


            return BaseRespVo.ok(addressBO.getId());

    }

    @RequestMapping("delete")
    public BaseRespVo delete( @RequestBody AddressId  id){
        wxAddressService.deleteById(id.getId());
        return BaseRespVo.ok();
    }

}
